import { C_ScrewColorEnum } from "../../C_Config";
import CollisionCallback from "../../component/C_CollisionCallback";
import C_SwitchFrame from "../../component/C_SwitchFrame";
import C_App from "../../core/C_App";
import { C_NotificationConst } from "../../core/C_NotificationConst";
import C_SoundManager, { SoundName } from "../../core/utils/C_SoundManager";
import C_Plate from "./C_Plate";

const { ccclass, property } = cc._decorator;
/** 螺丝孔 */
@ccclass
export default class C_Hole extends cc.Component {
    // @property()
    protected _curIndex: number = 0;

    // @property()
    set curIndex(v) {
        this._curIndex = v;
    }

    get curIndex() {
        return this._curIndex;
    }

    // @property()
    _color: number = 0;

    // @property({ type: cc.Enum(C_ScrewColorEnum) })
    set color(v: number) {
        this._color = v;
        this.screwFrame.frameIndex = v;
    }

    get color() {
        return this._color;
    }

    @property()
    _type: number = 0;
    @property()
    set type(v: number) {
        this._type = v;
    }

    get type() {
        return this._type;
    }

    @property(cc.Node)
    screwNode: cc.Node = null;

    @property(C_SwitchFrame)
    screwFrame: C_SwitchFrame = null;

    @property(cc.BoxCollider)
    boxCollision: cc.BoxCollider = null;

    @property(cc.PhysicsCollider)
    physicsBox: cc.PhysicsCollider = null;
    // @property(C_SwitchFrame)
    // holeFrame: C_SwitchFrame = null;

    topLayer: { [key: number]: number } = {};
    uId: number;
    /** 是否有螺丝 */
    isScrew: boolean = false;
    protected onLoad(): void {
        if (this._type === 0) {
            this.screwNode.on(cc.Node.EventType.TOUCH_END, this.onTouchOpen, this);
            const callBack = this.boxCollision.node.addComponent(CollisionCallback);
            callBack.onCollisionEnter = this.onCollisionEnter.bind(this);
            callBack.onCollisionExit = this.onCollisionExit.bind(this);
            // this.boxCollision.node.group = 'default';
            this.screwNode.active = true;
            this.isScrew = true;
        }
    }

    /** 禁止移动螺丝 */
    banOpen() {
        C_SoundManager.instance.playEffect(SoundName.Ban)
        cc.tween(this.screwFrame.node)
            .to(.05, { x: -3 })
            .to(.05, { x: 0 })
            .to(.05, { x: 3 })
            .to(.05, { x: 0 })
            .union()
            .repeat(3)
            .start()
    }

    onTouchOpen(event: cc.Event.EventTouch) {
        if (this.isScrew) {
            for (let key in this.topLayer) {
                if (this.topLayer[key] > 0) {
                    this.banOpen();
                    return;
                }
            }
            C_App.NotificationCenter.dispatch(C_NotificationConst.OPEN_SCREW, this, this.node.convertToWorldSpaceAR(this.screwNode.position))
        }
    }

    initHole(color?: C_ScrewColorEnum) {
        // this.holeFrame.frameIndex = color;
        this.screwNode.active = false;
    }

    addScrew(color: C_ScrewColorEnum) {
        this.screwNode.active = true;
        this.color = color;
        this.isScrew = true;

    }

    removeScrew() {
        C_SoundManager.instance.playEffect(SoundName.Open_Screw);
        this.screwNode.active = false;
        this.isScrew = false;
    }

    hasScrew() {
        return this.screwNode.active;
    }

    /**
    * 当碰撞产生的时候调用
    * @param  {Collider} other 产生碰撞的另一个碰撞组件
    * @param  {Collider} self  产生碰撞的自身的碰撞组件
    */
    protected onCollisionEnter(other: cc.BoxCollider | cc.PolygonCollider, self: cc.BoxCollider | cc.PolygonCollider) {
        // this.addTarget(RoleService.ins.mainCat)
        // CC_DEBUG && console.log('开始碰撞', other);
        const plate = other.node.parent.parent.getComponent(C_Plate);
        if (plate && plate.curIndex > this.curIndex) {
            // console.log("碰撞》》", plate.curIndex, this.curIndex)
            if (!this.topLayer[plate.curIndex]) {
                this.topLayer[plate.curIndex] = 1;
            }
            else {
                this.topLayer[plate.curIndex]++;
            }
        }
    }
    /**
   * 当碰撞结束后调用
   * @param  {Collider} other 产生碰撞的另一个碰撞组件
   * @param  {Collider} self  产生碰撞的自身的碰撞组件
   */
    protected onCollisionExit(other, self) {
        // CC_DEBUG && console.log('结束碰撞', other);

        const plate = other.node.parent.parent.getComponent(C_Plate);
        if (plate && plate.curIndex >= this.curIndex) {
            if (!this.topLayer[plate.curIndex]) {
                this.topLayer[plate.curIndex] = 0;
            }
            else {
                this.topLayer[plate.curIndex]--;
            }
        }
    }
}
